﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ViCode_LeVi.Data;
using ViCode_LeVi.Reports;

namespace ViCode_LeVi.Controls
{
    /// <summary>
    /// Interaction logic for VMTabBalanceAndOthers_Price.xaml
    /// </summary>
    public partial class VMTabBalanceAndOthers_Price : UserControl
    {
        public VMTabBalanceAndOthers_Price()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(VMTabBalanceAndOthers_Price_Loaded);
        }

        Step_BalanceAndOthers_Price _price = null;
        void VMTabBalanceAndOthers_Price_Loaded(object sender, RoutedEventArgs e)
        {
            _price = this.DataContext as Step_BalanceAndOthers_Price;
            GenUI(_price);
            SetupButton_AddNew();
        }

        #region GenUI
        private void GenUI(Step_BalanceAndOthers_Price price)
        {
            if (price == null)
                return;
            if (price.MaterialItems == null || price.MaterialItems.Length == 0)
            {
                price.AddMaterialItems(new Step_BalanceAndOthers_Price_Material());
            }
            GenUI_Material(StackPanel_Material, price.MaterialItems.ToArray());
            GenUI_Wages(StackPanel_Wages, price.WagesItems.ToArray());
            GenUI_DirectCosts(StackPanel_DirectCosts, price.DirectCostsItems.ToArray());            
        }
        private void GenUI_Material(Panel panel, Step_BalanceAndOthers_Price_Material[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_Price_Material()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_price != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _price.RemoveMaterialItems(removeItem);
                                        //removeItem.SendPropertyChanged_All();
                                        GenUI(_price);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }
        private void GenUI_Wages(Panel panel, Step_BalanceAndOthers_Price_Wages[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_Price_Wages()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_price != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _price.RemoveWagesItems(removeItem);
                                        //removeItem.SendPropertyChanged_All();
                                        GenUI(_price);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }

        private void GenUI_DirectCosts(Panel panel, Step_BalanceAndOthers_Price_DirectCosts[] items)
        {
            if (items == null || items.Length == 0)
            {
                panel.Children.Clear();
            }
            else
            {
                ////Collapse all
                //foreach(FrameworkElement child in panel.Children)
                //{
                //    child.Visibility = Visibility.Collapsed;
                //}
                //add
                var children = panel.Children.Cast<FrameworkElement>().ToArray();
                List<FrameworkElement> listInUI = new List<FrameworkElement>();
                foreach (var item in items)
                {
                    var child = children.FirstOrDefault(d => d.DataContext == item);
                    if (child == null)
                    {
                        child = new VMTabBalanceAndOthers_Price_DirectCosts()
                        {
                            DataContext = item
                              ,
                            Action_Remove = (removeItem) =>
                            {
                                if (_price != null)
                                {
                                    if (MessageBox.Show("Do you want to remove this item?", "Confirm", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                                    {
                                        _price.RemoveDirectCostsItems(removeItem);
                                        //removeItem.SendPropertyChanged_All();
                                        GenUI(_price);
                                    }
                                }
                            }
                        };
                        panel.Children.Add(child);
                    }
                    listInUI.Add(child);
                }
                //remove old
                List<FrameworkElement> listWillRemove = new List<FrameworkElement>();
                foreach (FrameworkElement child in panel.Children)
                {
                    if (!listInUI.Contains(child))
                        listWillRemove.Add(child);
                }
                foreach (var removeItem in listWillRemove)
                {
                    panel.Children.Remove(removeItem);
                }
            }
        }
        #endregion

        #region Button add new
        private void SetupButton_AddNew()
        {
            Button_AddNew_Material.Click -= new RoutedEventHandler(Button_AddNew_Material_Click);
            Button_AddNew_Material.Click += new RoutedEventHandler(Button_AddNew_Material_Click);

            Button_AddNew_Wages.Click -= new RoutedEventHandler(Button_AddNew_Wages_Click);
            Button_AddNew_Wages.Click += new RoutedEventHandler(Button_AddNew_Wages_Click);

            Button_AddNew_DirectCosts.Click -= new RoutedEventHandler(Button_AddNew_DirectCosts_Click);
            Button_AddNew_DirectCosts.Click += new RoutedEventHandler(Button_AddNew_DirectCosts_Click);
        }

        void Button_AddNew_DirectCosts_Click(object sender, RoutedEventArgs e)
        {
            if (_price != null)
                _price.AddDirectCostsItems(new Step_BalanceAndOthers_Price_DirectCosts());
            GenUI(_price);
        }

        void Button_AddNew_Wages_Click(object sender, RoutedEventArgs e)
        {
            if (_price != null)
                _price.AddWagesItems(new Step_BalanceAndOthers_Price_Wages());
            GenUI(_price);
        }

        void Button_AddNew_Material_Click(object sender, RoutedEventArgs e)
        {
            if (_price != null)
                _price.AddMaterialItems(new Step_BalanceAndOthers_Price_Material());
            GenUI(_price);
        }
        #endregion

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Report_Price_Calculation rep = new Report_Price_Calculation();
            var datasource = new List<Step_BalanceAndOthers_Price>();
            datasource.Add(_price);
            frmViewReport frm = new frmViewReport(rep, datasource);
            frm.Show();
        }
    }
}
